package com.slh.touchandroid.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.slh.touchandroid.R;
import com.slh.touchandroid.adapter.FunctionApdater;
import com.slh.touchandroid.model.FloatInfo;
import com.slh.touchandroid.receiver.Broadcast;
import com.slh.touchandroid.utils.CONFIG;

public class ChangeIconActivity extends BaseActivity implements
		OnItemClickListener {
	private GridView iconGridView;
	private FunctionApdater iconApdater;
	private ArrayList<FloatInfo> infos = new ArrayList<FloatInfo>();

	public static void start(Context context) {
		Intent intent = new Intent(context, ChangeIconActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_change_icon);
		iconGridView = (GridView) findViewById(R.id.grid);
		loadIcon();

	}

	private void loadIcon() {
		for (int i = 0; i < 14; i++) {
			FloatInfo info = new FloatInfo("", getResources().getIdentifier(
					"ic_android" + i, "drawable", getPackageName()), "");
			infos.add(info);
		}
		iconApdater = new FunctionApdater(this, R.layout.row_common_vertical,
				infos);
		iconGridView.setAdapter(iconApdater);
		iconGridView.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		CONFIG.FLOAT_ICON.setFloatIcon(ChangeIconActivity.this,
				infos.get(position).resource);
		Broadcast.sendBroadcast(ChangeIconActivity.this,
				CONFIG.BROADCAST.ACTION_FLOAT_ICON_RESOURCE_CHANGED,
				CONFIG.BROADCAST.EXTRA_FLOAT_ICON_RESOURCE,
				infos.get(position).resource);

	}
}
